package com.sky.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.domain.Feedback;
import com.sky.domain.User;
import com.sky.dto.FeedBackDto;
import com.sky.dto.FeedBackPageDto;
import com.sky.mapper.FeedbackMapper;
import com.sky.service.FeedBackService;
import com.sky.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author majinan
 */
@Service
public class FeedBackServiceImpl implements FeedBackService {

    @Autowired
    private FeedbackMapper mapper;


    @Override
    public DataGridView selectList(FeedBackPageDto dto) {
        Page<Feedback> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<Feedback> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dto.getCreateByUser()), Feedback.COL_USER_NAME, dto.getCreateByUser());
        if (dto.getStatus() != null){
            qw.eq(StringUtils.isNotBlank(String.valueOf(dto.getStatus())), Feedback.COL_STATUS, dto.getStatus());
        }
        qw.ge(null!= dto.getBeginTime(), Feedback.COL_CREATE_TIME, dto.getBeginTime());
        qw.le(null!= dto.getEndTime(), Feedback.COL_CREATE_TIME, dto.getEndTime());
        qw.orderByAsc(Feedback.COL_USER_ID);
        this.mapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public Feedback selectById(Integer feedBackID) {
        return mapper.selectById(feedBackID);
    }

    @Override
    public int updateFeedBack(FeedBackDto dto) {
        Feedback temp = selectById(dto.getId());
        if (temp == null) {
            return 0;
        }
        temp.setStatus(2);
        temp.setModifyByName(dto.getModifyByName());
        temp.setModifyByUser(dto.getModifyByUser());
        temp.setModifyUrl(dto.getModifyUrl());
        temp.setRemark(dto.getRemark());
        return this.mapper.updateById(temp);
    }

    @Override
    public int insertFeedBack(FeedBackDto dto) {
        Feedback feedback  = new Feedback();
        BeanUtils.copyProperties(dto, feedback);
        feedback.setStatus(1);
        return this.mapper.insert(feedback);
    }

    @Override
    public int deleteFeedBack(Long[] questionIds) {
        List<Long> ids = Arrays.asList(questionIds);
        return this.mapper.deleteBatchIds(ids);
    }

    @Override
    public int feedBackCount() {
        return 0;
    }
}
